package com.lanxin.oa.zu.service.serviceImpl;

import com.lanxin.bean.*;
import com.lanxin.oa.zu.dao.KehuDao;
import com.lanxin.oa.zu.service.KehuService;
import com.lanxin.util.PageBean;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class KehuServiceImpl implements KehuService {
    @Autowired
    KehuDao dao;

    @Override
    public PageBean selectcp(Integer page, Integer pageSize, String linkname) {
        return dao.selectcp(page, pageSize, linkname);
    }

    @Override
    public KhProduct selectcpByid(Integer oid) {
        return dao.selectcpByid(oid);
    }

    @Override
    public void insertcp(KhProduct cp, HttpServletRequest request, MultipartFile imgfile) {
        if (cp.getOid()!=null){
           dao.update(cp);
        }else {
            if (imgfile != null) {
                String str = upload(request, imgfile);
                cp.setPicture(str);
            }
            dao.add(cp);
        }

    }


    @Override
    public boolean selectcpByName(String linkname, Integer oid) {
        KhProduct cp = dao.selectcpName(linkname, oid);
        if (cp != null) {
            return true;
        }
        return false;
    }
    //文件上传
    public String upload(HttpServletRequest request, MultipartFile accpath) {
        String str = "";
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replaceAll("-", "");
        String dir = request.getServletContext().getRealPath("upload");
        if (accpath.getSize() > 0) {
            File le = new File(dir + "/" + uuid);
            le.mkdir();
            str = uuid + "/" + accpath.getOriginalFilename();
            File newfile = new File(le, accpath.getOriginalFilename());
            try {
                FileUtils.copyInputStreamToFile(accpath.getInputStream(), newfile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return str;
    }

    @Override
    public boolean deletecp(KhProduct cp) {
        KhCustomerfavorite khgx=dao.selectgx(cp.getOid());
        if (khgx!= null ) {
            return false;
        }
        dao.delete(cp);
        return  true;
    }
///////////////////////////客户///////////////////////////////////
    @Override
    public PageBean selectkh(Integer page, Integer pageSize, String customerName, String phone, Integer companyType, Integer vocation) {
        return dao.selectkh(page,pageSize,customerName,phone,companyType,vocation);
    }

    @Override
    public List selectxz() {
        return dao.selectxz();
    }

    @Override
    public List selecthy() {
        return dao.selecthy();
    }

    @Override
    public List selectxy() {
        return  dao.selectxy();
    }

    @Override
    public void insertkehu(KhCustomer kh, String[] contactName, String[] job, String[] cphone, String[] cemail, String[] czipcode, String[] caddress) {

        if (contactName != null) {
            for (int i = 0; i < contactName.length; i++) {
                KhCustomercontact khlxr=new KhCustomercontact();
//                khlxr.setCustomerid(kh.getOid());
                khlxr.setContactName(contactName[i]);
                khlxr.setJob(job[i]);
                khlxr.setPhone(cphone[i]);
                khlxr.setEmail(cemail[i]);
                khlxr.setZipcode(czipcode[i]);
                khlxr.setAddress(caddress[i]);
                kh.getKulxr().add(khlxr);
            }
        }
        dao.add(kh);
    }

    @Override
    public KhCustomer selectkhById(Integer oid) {
        return dao.selectkhById(oid);
    }

    @Override
    public void updatekehu(KhCustomer kh, Integer[] coid, String[] contactName, String[] job, String[] cphone, String[] cemail, String[] czipcode, String[] caddress) {
        kh.getKulxr().clear();

        if (contactName != null) {
            for (int i = 0; i < contactName.length; i++) {
                KhCustomercontact khlxr=new KhCustomercontact();
                if (coid != null && coid.length > 0) {
                    if (i < coid.length) {
                        khlxr.setOid(coid[i]);
                    }
                }
//                khlxr.setCustomerid(kh.getOid());
                khlxr.setContactName(contactName[i]);
                khlxr.setJob(job[i]);
                khlxr.setPhone(cphone[i]);
                khlxr.setEmail(cemail[i]);
                khlxr.setZipcode(czipcode[i]);
                khlxr.setAddress(caddress[i]);
                kh.getKulxr().add(khlxr);
            }
        }
        dao.update(kh);

    }

    @Override
    public PageBean selectkhgxcp(Integer customerid, Integer page, Integer pageSize) {
        return dao.selectkhgxcp(customerid,page,pageSize);
    }

    @Override
    public List selectcp() {
        return dao.selectcp();
    }

    @Override
    public void insertgxcp(KhCustomerfavorite gx) {
        dao.add(gx);
    }

    @Override
    public boolean guanxin(Integer customerid, Integer productid) {
        KhCustomerfavorite gx=dao.guanxin(customerid,productid);
        if (gx != null) {
            return true;
        }
        return false;
    }

    @Override
    public void updateguanxin(KhCustomerfavorite gx) {
        dao.update(gx);
    }

    @Override
    public List selectlxr(Integer oid) {
        return dao.selectlxr(oid);
    }

    @Override
    public void insertgenzong(KhContactrecord gz) {
        gz.setContactDate(new Date());
        dao.add(gz);
    }

    @Override
    public PageBean selectgzjl(Integer oid, Integer page, Integer pageSize) {
        return dao.selectgzjl(oid,page,pageSize);
    }


}


